package control.panel
{
	import events.TableEvent;
	
	import flash.events.TimerEvent;
	
	import utils.EnterFrameTimer;
	
	import view.panel.result.ResultPanel;

	public class ResultPanelControl extends PanelControl
	{
		private var timer:EnterFrameTimer;
		private var leftTime:int = 4;
		
		public function ResultPanelControl()
		{
			initPanel();
			initListener();
			initTimer();
		}
		
		private function initTimer():void
		{
			timer = new EnterFrameTimer(1000);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			showCount();
			if (leftTime < 0)
			{
				timer.stop();
				_view.resultPanel.close();
			}
			leftTime--;
		}
		
		private function showCount():void
		{
			_view.resultPanel.updateAutoClose("<font color='#FF0000' size='12'>" + leftTime +" <font color='#AAAAAA'>秒后自动关闭</font></font>");
		}
		
		private function initPanel():void
		{
			_view.resultPanel = new ResultPanel(this);
		}
		
		private function initListener():void
		{
			_model.tableModel.addEventListener(TableEvent.ROUND_RESULT,onRoundResult);
		}
		
		private function onRoundResult(event:TableEvent):void
		{
			_view.resultPanel.updateResult(event.roundResultData);
			
			leftTime = 4;
			timer.start();
			showCount();
			_view.resultPanel.open();
		}
		
		public function clear():void
		{
			_view.resultPanel.clear();
			timer.stop();
		}
		
		
	}
}